package com.bjyhl.medical.main.video;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.bjyhl.core.fragments.LatteFragment;
import com.bjyhl.core.fragments.bottom.BaseBottomFragment;
import com.bjyhl.core.net.RestClient;
import com.bjyhl.core.net.callback.IError;
import com.bjyhl.core.net.callback.IFailure;
import com.bjyhl.core.net.callback.ISuccess;
import com.bjyhl.medical.R;
import com.bjyhl.ui.recycler.ItemType;
import com.bjyhl.ui.recycler.MultipleFields;
import com.bjyhl.ui.recycler.MultipleItemEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.SimpleClickListener;

import java.util.List;

/**
 * Created by Natalya on 2019/1/27 18:22
 * Note: 主页fragment
 */

public class VideoBlockFragment extends LatteFragment{

    public static final String ARG_VIDEO_TYPE = "ARG_VIDEO_TYPE";
    public static final String ARG_VIDEO_COUNT = "ARG_VIDEO_COUNT";
    private RecyclerView mRecyclerView = null;

    private Integer mVideoCount = 0;
    private String mVideoType = null;
    private BaseBottomFragment baseBottomFragment;

    public static VideoBlockFragment newInstance(String videoType, Integer videoCount, BaseBottomFragment baseBottomFragment) {
        final Bundle args = new Bundle();
        args.putString(ARG_VIDEO_TYPE, videoType);
        args.putInt(ARG_VIDEO_COUNT, videoCount);
        final VideoBlockFragment fragment = new VideoBlockFragment();
        fragment.baseBottomFragment = baseBottomFragment;
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public Object setLayout() {
        return R.layout.fragment_video_list;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View root) {
        mRecyclerView = $(R.id.rv_video_list);
        initRecyclerView();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle args = getArguments();
        if (args != null) {
            mVideoCount = args.getInt(ARG_VIDEO_COUNT);
            mVideoType = args.getString(ARG_VIDEO_TYPE);
        }
    }

    private void initRecyclerView() {
        if (mVideoCount > ItemType.VIDEO_DEFAULT_COUNT) {
            final GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 6);
            mRecyclerView.setLayoutManager(layoutManager);
        } else {
            final LinearLayoutManager manager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
            mRecyclerView.setLayoutManager(manager);
        }

//        final VideoOneTypeFragment videoFragmentOneType = getParentFragments();
//        final VideoFragment videoFragment = videoFragmentOneType.getParentFragments();
//        final EcBottomFragment baseBottomFragment = videoFragment.getParentFragments();
        mRecyclerView.addOnItemTouchListener(new SimpleClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MultipleItemEntity entity = (MultipleItemEntity) (adapter.getData().get(position));
                final String videoUrl = entity.getField(MultipleFields.CONTENT);
                final String videoFullTitle = entity.getField(MultipleFields.TEXT);
                Boolean is_cat = entity.getField(MultipleFields.TAG);
                if (is_cat) {
                    // 专辑页面
                    VideoCategoryFragment categoryFragment = VideoCategoryFragment.newInstance(videoFullTitle, entity.getField(MultipleFields.ID), baseBottomFragment);
                    baseBottomFragment.getSupportDelegate().start(categoryFragment);
                } else {
                    // 弹出视频播放页
                    VideoCastFragment videoCastFragment = VideoCastFragment.create(videoUrl, videoFullTitle);
//                getSupportDelegate().start(videoCastFragment);
                    baseBottomFragment.getSupportDelegate().start(videoCastFragment);
                }
//                Toast.makeText(getContext(), videoUrl, Toast.LENGTH_SHORT);
            }

            @Override
            public void onItemLongClick(BaseQuickAdapter adapter, View view, int position) {}

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {}

            @Override
            public void onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {}
        });
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);

        RestClient.builder()
                .url("http://192.168.8.101:3000/v1/movie/getMoviesByTag/"+mVideoType+"/"+mVideoCount)
                .loader(getContext())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        final List<MultipleItemEntity> data =
                                new VideoListDataConverter().setJsonData(response).convert();
                        final VideoListRecyclerAdapter adapter = new VideoListRecyclerAdapter(data);
                        mRecyclerView.setAdapter(adapter);
                    }

                })
                .failure(new IFailure() {
                    @Override
                    public void onFailure() {
                        Toast.makeText(getContext(), "加载" + mVideoType + "列表Failure", Toast.LENGTH_LONG).show();
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        Toast.makeText(getContext(), "加载" + mVideoType + "列表Error" + "("+code+")" + msg, Toast.LENGTH_LONG).show();
                    }
                })
                .build()
                .get();
    }
}
